﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Collections;
namespace PTTK11_HuongRung
{
    public partial class frmCapNhatMonAn : Form
    {
        public frmCapNhatMonAn()
        {
            InitializeComponent();
        }

        private void frmCapNhatMonAn_Load(object sender, EventArgs e)
        {
            btnDangNhap.Text = "Đăng Xuất";
            NhanVienBUS nvBus = new NhanVienBUS();
            NhanVienDTO nvDto = new NhanVienDTO();
            nvDto = nvBus.GetThongTinNhanVien(Secssion.MaNV);
            txtHoTen.Text = nvDto.TENNHANVIEN.Trim();
            LoaiNhanVienBUS lnvbus = new LoaiNhanVienBUS();
            txtMaNV.Text = lnvbus.GetTenLoaiNV(nvDto.LOAINHANVIEN);
            reLoadMonAn();
        }
        private void reLoadMonAn()
        {
            MonAnBUS bus = new MonAnBUS();
            cbxMaMonAn.DataSource = bus.GetDSMonAn();
            cbxMaMonAn.DisplayMember = "TENMONAN";
            cbxMaMonAn.ValueMember = "MONANID";
            //cbxMaMonAn.SelectedIndex = 0;
            cbxMaMonAn.Text = "";
            txtTenMonAn.Text = "";
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmMonAn frm = new frmMonAn();
            frm.ShowDialog();
            this.Close();
        }

        private void btCapNhat_Click(object sender, EventArgs e)
        {

        }

        private void cbxMaMonAn_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTenMonAn.Text = "";
            
            if (cbxMaMonAn.Text.Count() < 0 || cbxMaMonAn.SelectedIndex < 0)            
                return;
            txtTenMonAn.Text = cbxMaMonAn.Text;
            lbtieude.Text ="NGUYÊN VẬT LIỆU CHO MÓN ĂN "+ cbxMaMonAn.Text.ToUpper();
            if(cbxMaMonAn.SelectedValue.ToString()=="DTO.MonAnDTO")
                return;
            reLoadNguyenVatLieu(int.Parse(cbxMaMonAn.SelectedValue.ToString()));
            reLoadNguyenVatLieu();

        }
        public int ma;
        public int manvl;
        private void reLoadNguyenVatLieu(int mamonan)
        {
            ChiTietMonAnBUS bus = new ChiTietMonAnBUS();

            bus.GetChiTietMonAn(mamonan);
            List<ChiTietMonAnDTO> lis = new List<ChiTietMonAnDTO>();
            lis = bus.GetChiTietMonAn(mamonan);
            dgvDanhSach.Rows.Clear();
            for (int i = 0; i < lis.Count(); i++)
            {
                NguyenVatLieuBUS nvl = new NguyenVatLieuBUS();
                string tenNVL = nvl.GetTenNVL(lis[i].NVLID);

                dgvDanhSach.Rows.Add(i + 1, lis[i].NVLID, tenNVL, lis[i].SOLUONGPHANAN);

            }

            ma = int.Parse( cbxMaMonAn.SelectedValue.ToString());
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            if (KTTonTai(cbxTenNVL.Text.Trim()) == true)
            {
                MessageBox.Show("nguyen vật liệu đã có.");
                return;
            }

            ChiTietMonAnDTO dto = new ChiTietMonAnDTO();
            dto.MONANID = ma;
            dto.NVLID=int.Parse(cbxTenNVL.SelectedValue.ToString());
            dto.SOLUONGPHANAN = float.Parse(txtSoluong.Text.Trim());
            ChiTietMonAnBUS bus = new ChiTietMonAnBUS();
            bus.ThemMonAn(dto);
            reLoadNguyenVatLieu(int.Parse(cbxMaMonAn.SelectedValue.ToString()));
            MessageBox.Show("đã thêm mới nguyên vật liệu");
        }

        private bool KTTonTai(string p)
        {
            for (int i = 0; i < dgvDanhSach.Rows.Count; i++)
            {
                if (dgvDanhSach.Rows[i].Cells[2].ToString().CompareTo(p) == 0)
                    return true;
            }
            return false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            MonAnBUS bus = new MonAnBUS();
            MonAnDTO dto = new MonAnDTO();
            dto.MONANID = ma;
            dto.TENMONAN = txtTenMonAn.Text.Trim();         
            bus.CapNhatMonAn(dto);
            reLoadMonAn();
            dgvDanhSach.Rows.Clear();
            MessageBox.Show("Thay đổi tên món ăn thành công.");

        }

        private void dgvDanhSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;

            cbxTenNVL.SelectedValue = int.Parse(dgvDanhSach.Rows[e.RowIndex].Cells[1].Value.ToString());
            txtSoluong.Text = dgvDanhSach.Rows[e.RowIndex].Cells[3].Value.ToString();
        }
        private void reLoadNguyenVatLieu()
        {
            NguyenVatLieuBUS bus = new NguyenVatLieuBUS();
            cbxTenNVL.DataSource = bus.GetNguyenVatLieu();
            cbxTenNVL.DisplayMember = "TEN_NGUYEN_VAT_LIEU";
            cbxTenNVL.ValueMember = "NGUYEN_VAT_LIEU_ID";
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            ChiTietMonAnDTO dto= new ChiTietMonAnDTO();
            ChiTietMonAnBUS bus = new ChiTietMonAnBUS();
            dto.MONANID = ma;
            dto.NVLID =int.Parse(cbxTenNVL.SelectedValue.ToString());
            dto.SOLUONGPHANAN = float.Parse(txtSoluong.Text.Trim());
            bus.CapNhatChiTietMonAn(dto);
            dgvDanhSach.Rows.Clear();
            reLoadNguyenVatLieu(int.Parse(cbxMaMonAn.SelectedValue.ToString()));
            MessageBox.Show("Đã thay đổi nguyên vật liệu");
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            ChiTietMonAnBUS bus = new ChiTietMonAnBUS();
            bus.XoaChiTietMonAn(int.Parse(cbxMaMonAn.SelectedValue.ToString()), int.Parse(cbxTenNVL.SelectedValue.ToString()));
            reLoadNguyenVatLieu(int.Parse(cbxMaMonAn.SelectedValue.ToString()));
            MessageBox.Show("xóa thành công nguyên vật liệu" );
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text.Trim().CompareTo("Đăng Xuất") == 0)
            {
                btnDangNhap.Text = "Đăng Nhâp";
                Secssion.MaNV = "";
                Secssion.MaKhac = "";
                
                txtHoTen.Text = "";
                txtMaNV.Text = "";
                this.Hide();
                frmQuanLy frm = new frmQuanLy();
                frm.ShowDialog();
                this.Close();
            }
            else
            {
                this.Hide();
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();
                this.Close();
            }
        }


    }
}
